﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OracleTools.OracleHomeSelector.Properties;

namespace OracleTools.OracleHomeSelector
{
    public class SavedNlsLangs
    {
        private static SavedNlsLangs m_this = null;

        private List<string> m_lstLangsLCase;
        public List<string> NlsLangs;


        private SavedNlsLangs()
        {
            m_lstLangsLCase = new List<string>();
            NlsLangs = new List<string>();

            try
            {
                if (Settings.Default.MyNlsLangs != null)
                {
                    string[] array = new string[Settings.Default.MyNlsLangs.Count];
                    Settings.Default.MyNlsLangs.CopyTo(array, 0);
                    foreach (string str in array)
                    {
                        this.NlsLangs.Add(str);
                        this.m_lstLangsLCase.Add(str.ToLower());
                    }
                }
            }
            catch (Exception) { }
        }

        public static SavedNlsLangs GetInstance()
        {
            if (m_this == null)
                m_this = new SavedNlsLangs();
            return m_this;
        }

        public void Clear()
        {
            this.NlsLangs.Clear();
            this.m_lstLangsLCase.Clear();
            this.Sync();
        }
        public int AddItem(string value)
        {
            if (string.IsNullOrWhiteSpace(value.Trim()))
            {
                return -1;
            }
            if (this.m_lstLangsLCase.Contains(value.ToLower().Trim()))
            {
                return -1;
            }
            this.NlsLangs.Add(value.Trim());
            this.m_lstLangsLCase.Add(value.ToLower().Trim());
            this.Sync();
            return this.NlsLangs.IndexOf(value.Trim());

        }
        public void RemoveItem(string value)
        {
            if (!string.IsNullOrWhiteSpace(value.Trim()) && this.m_lstLangsLCase.Contains(value.ToLower().Trim()))
            {
                this.NlsLangs.Remove(value.Trim());
                this.m_lstLangsLCase.Remove(value.ToLower().Trim());
                this.Sync();
            }

        }

        private void Sync()
        {
            this.NlsLangs.Sort();
            try
            {
                Settings.Default.MyNlsLangs.Clear();
                Settings.Default.MyNlsLangs.AddRange(this.NlsLangs.ToArray());
                Settings.Default.Save();
            }
            catch (Exception) { }
        }

    }
}
